package com.shire.shire_interface.service.commandcenter;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.constants.WxConstants;
import com.shire.shire_interface.constants.WxErrorCode;
import com.shire.shire_interface.dao.command.WxCommandDao;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.friend.WxFriendLog;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupMember;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.service.friend.WxFriendLogService;
import com.shire.shire_interface.service.group.WxGroupMemberService;
import com.shire.shire_interface.service.group.WxGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WxCommandService {
    private static Logger logger = LoggerFactory.getLogger(WxCommandService.class);
    @Autowired
    private WxCommandDao commandDao;
    @Autowired
    private WxGroupService groupService;
    @Autowired
    private WxGroupMemberService groupMemberService;
    @Autowired
    private WxFriendLogService friendLogService;

    public WxCommand addWxCommand(Integer csId, Integer accountId, WxConstants.CmdType cmdType, String reqContent, Date startTime){
        if(startTime==null){
            startTime = new Date();
        }
        return generateCmdObject(csId, accountId, reqContent,WxConstants.CmdTaskType.UNKNOWN, 0, cmdType, WxConstants.CmdPriority.UNKNOWN);
    }

    public WxCommand genSyncAddressCmd(WxAccount account){
        return generateCmdObject(account.getCsid(),
                account.getId(),
                "{}",
                WxConstants.CmdTaskType.UNKNOWN,
                -1,WxConstants.CmdType.CRK_SYNC_ADDRESS,
                WxConstants.CmdPriority.UNKNOWN);

    }

    public WxCommand generateCmdObject(Integer csId,
                                                Integer accountId,
                                                String reqContent,
                                                WxConstants.CmdTaskType taskType,
                                                Integer taskId,
                                                WxConstants.CmdType cmdType,
                                                WxConstants.CmdPriority priority){
        WxCommand wxCommand = new WxCommand();
        wxCommand.setCsid(csId);
        wxCommand.setAccountid(accountId);
        wxCommand.setReqcontent(reqContent);
        wxCommand.setTasktype(taskType.getCode());
        wxCommand.setTaskid(taskId);
        wxCommand.setCmdtype(cmdType.getCode());
        wxCommand.setCmdstatus(WxConstants.CmdStatus.NEW.getCode());
        wxCommand.setRststatus(WxErrorCode.INIT_VALUE);
        wxCommand.setPriority(priority.getCode());
        wxCommand.setTrytimes(0);
        wxCommand.setStarttime(new Date());
        wxCommand.setEndtime(new Date(wxCommand.getStarttime().getTime()+10*3600*1000L));
        commandDao.insert(wxCommand);
        return wxCommand;
    }

    public WxCommand getCommandById(Integer cmdId){
        return commandDao.selectByPrimaryKey(cmdId.longValue());
    }

    public void updateWxCommand(WxCommand cmd){
        cmd.setUpdatetime(new Date());
        commandDao.updateByPrimaryKeySelective(cmd);
    }


    public WxCommandDao getCommandDao() {
        return commandDao;
    }

    public void setCommandDao(WxCommandDao commandDao) {
        this.commandDao = commandDao;
    }

    public WxCommand selectByPrimaryKey(Long cmdid) {
        return commandDao.selectByPrimaryKey(cmdid);
    }
    public WxCommand getAddGroupMemberCmd(WxAccount wxAccount, String groupNo, String memberWxid, String applyMsg, WxConstants.CmdTaskType taskType, Integer taskId) {
        WxCommand cmd;
        WxGroup group = groupService.selectGroupByNoForCs(wxAccount.getCsid(),wxAccount.getWxid(),groupNo);
        WxGroupMember member = groupMemberService.getGroupMember(wxAccount.getCsid(),group.getGroupno(), memberWxid);
        if (member == null) {
            logger.info("群用户信息不存，添加好失败 csId:" + wxAccount.getCsid()
                    + " groupNo+" + group.getGroupno()
                    + " wxid:" + memberWxid);
            return null;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("groupWxId", group.getGroupno());
        jsonObject.put("userWxId", memberWxid);
        jsonObject.put("applyMsg", applyMsg);
        cmd = generateCmdObject(wxAccount.getCsid(),
                wxAccount.getId(),
                jsonObject.toJSONString(),
                taskType,
                taskId,
                WxConstants.CmdType.CRK_ADD_FRIEND,
                WxConstants.CmdPriority.INVITE);
        // 添加好友日志
        WxFriendLog wxFriendLog = new WxFriendLog();
        wxFriendLog.setCmdid(cmd.getId());
        wxFriendLog.setCmdstatus(cmd.getCmdstatus());
        wxFriendLog.setCmdtype(WxConstants.CmdType.CRK_ADD_FRIEND.getCode());
        wxFriendLog.setWxaccount(wxAccount.getWxid());
//                wxFriendLog.setGroupid(groupId);
        wxFriendLog.setGroupname(group.getGroupname());
        wxFriendLog.setFriendwxid(memberWxid);
        wxFriendLog.setFriendnick(member.getNick());
        wxFriendLog.setAddtype(WxConstants.FriendAddType.FROMGROUP.getCode());
        wxFriendLog.setRststatus(cmd.getRststatus());
        wxFriendLog.setApplymsg(applyMsg);
        wxFriendLog.setCsid(wxAccount.getCsid());
        wxFriendLog.setCreatetime(new Date());
        wxFriendLog.setUpdatetime(wxFriendLog.getCreatetime());
        wxFriendLog.setTasktype(taskType.getCode());
        wxFriendLog.setTaskid(taskId);
        wxFriendLog.setAddresult(0);
        friendLogService.inserFriendLog(wxFriendLog);
        return cmd;
    }
}
